#include "mainwindow.h"
#include "QMessageBox"
#include <chrono>
#include <ctime>
#include <string>
using namespace std;
int MainWindow::winner(){
    bool blackWin = true,whiteWin = true;
    for (int i = 0; i < 4 ;i++ ) {
        for (int j = 0;j < 8 ;j++ ) {
            if(cb_with_num.chessborad[cb_with_num.movechess.blackchess[i].i + dection[j]] == 2){
                whiteWin = false;
                goto next;
            }
        }
    }
next:
    for (int i = 0; i < 4 ;i++ ) {
        for (int j = 0;j < 8 ;j++ ) {
            if(cb_with_num.chessborad[cb_with_num.movechess.whitechess[i].i + dection[j]] == 2){
                blackWin = false;
                goto end;
            }
        }
    }
end:
    if(blackWin) return blackc;
    else if(whiteWin) return whitec;
    else return 2;
}

int Engine::winner(const Cb_with_num &cb){
    bool blackWin = true,whiteWin = true;
    for (int i = 0; i < 4 ;i++ ) {
        for (int j = 0;j < 8 ;j++ ) {
            if(cb.chessborad[cb.movechess.blackchess[i].i + dection[j]] == 2){
                whiteWin = false;
                goto next;
            }
        }
    }
next:
    for (int i = 0; i < 4 ;i++ ) {
        for (int j = 0;j < 8 ;j++ ) {
            if(cb.chessborad[cb.movechess.whitechess[i].i + dection[j]] == 2){
                blackWin = false;
                goto end;
            }
        }
    }
end:
    if(blackWin) return blackc;
    else if(whiteWin) return whitec;
    else if(blackWin && whiteWin) return 3;
    else return 2;
}

string gettime(){
    chrono::system_clock::time_point tp = chrono::system_clock::now();
    time_t t = chrono::system_clock::to_time_t(tp);
    char ts[100];
    ctime_s(ts, 100, &t);
    string s = ts;
    int sub = 0;
    while(s[sub] != '\n') sub++;
    s = s.substr(0,sub);
    for(int i = 0; i < s.length();i++)
        if (s[i] == ':') s[i] = '_';
    return s;
}

bool MainWindow::end_game(){
    win = winner();
    if(win != 2 && save){
        qDebug() << cb_save.size();
        save_t = new class thread([this](){

            ofstream out_text("C:\\AAAAA\\wu111-1-amazon-chess-dev\\amazon-chess\\ChessBorad\\data_text\\" + gettime() + ".txt");
            ofstream out_binary("C:\\AAAAA\\wu111-1-amazon-chess-dev\\amazon-chess\\ChessBorad\\data_binary\\" + gettime() + ".dat",ios::binary);
            if(!out_text.is_open()) qDebug() << "out_text con't open";
            for(auto &i:step_save){
                out_binary.write((char *)&i.oldchess,2);
                out_binary.write((char *)&i.chess,2);
                out_binary.write((char *)&i.hinder,2);
                out_text << i.oldchess << " " << i.chess << " " << i.hinder <<  '\n';
            }
            auto win_s = (short)win;
            out_binary.write((char *)&win_s,2);
            if(win == blackc) out_text << "黑方胜利";
            else out_text << "白方胜利";
            out_text.close();
            out_binary.close();
        });
        save_t->detach();
    }
    if(win == 1){
        QMessageBox::information(this,"游戏结束","黑方获胜");
        current.time = 0;
        isfin = true;
        black_timer->pause();
        white_timer->pause();
        return true;
    }
    else if (win == 0) {
        QMessageBox::information(this,"游戏结束","白方获胜");
        current.time = 0;
        isfin = true;
        white_timer->pause();
        black_timer->pause();
        return true;
    }
    else if (win == 3){
        QMessageBox::information(this,"游戏结束","平局");
        current.time = 0;
        isfin = true;
        white_timer->pause();
        black_timer->pause();
        return true;
    }
    return false;
}
